package com.base;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.constant.Constant;
import com.ddd.wandemo.R;
import com.trello.rxlifecycle2.LifecycleTransformer;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;
import com.util.SpUtil;

public abstract class BaseActivity<T extends BaseContract.RootPresenter> extends RxAppCompatActivity implements BaseContract.RootView {
    public Context mContext;
    public T mPresenter;
    private FrameLayout mEmptyView;
    public int layoutId = 0;
    public int mAccentColor;
    private boolean mIsNight;
    private int mTheme;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        mIsNight = SpUtil.getBoolean(Constant.IS_NIGHT);
        mTheme = SpUtil.getInt(Constant.THEME);
        if (!mIsNight) {
            switch (mTheme) {
                case 0:
                    setTheme(R.style.BaseAppTheme);
                    break;
                case 1:
                    setTheme(R.style.AppTheme_First_Teal);
                    break;
                case 2:
                    setTheme(R.style.AppTheme_Green);
                    break;
                case 3:
                    setTheme(R.style.AppTheme_Purple);
                    break;
                default:
                    break;
            }

        }
        beforeSetContent();
        super.onCreate(savedInstanceState);
        layoutId = setLayoutId();
        setContentLayout();
        mContext = this;
        mPresenter = initPresenter();
        if (mPresenter != null) {
            mPresenter.attachView(this);
        }

        initStatusBar();
        initView();
        initEvent();
        afterSetContent();
    }

    protected void initStatusBar() {

        Window window = getWindow();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            TypedArray array = getTheme().obtainStyledAttributes(new int[] {
                R.attr.theme_color,R.attr.colorAccent
            });
            int backgroundColor = array.getColor(0, 0xFFFFFF);
            mAccentColor = array.getColor(1,0xFFFFFF);
            array.recycle();

            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(backgroundColor);
            View decor = window.getDecorView();
            int ui = decor.getSystemUiVisibility();
            ui |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (mTheme > 0) {
                    ui &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
                } else {
                    ui |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
                }
            }
            decor.setSystemUiVisibility(ui);
        } else {
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            ViewGroup systemContent = findViewById(android.R.id.content);
            View statusBarView = new View(this);
            ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight());
            statusBarView.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
            systemContent.getChildAt(0).setFitsSystemWindows(true);
            systemContent.addView(statusBarView, 0, lp);
        }
    }

    public int getStatusBarHeight() {
        int statusBarHeight1 = -1;
        //获取status_bar_height资源的ID
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            statusBarHeight1 = getResources().getDimensionPixelSize(resourceId);
        }
        return statusBarHeight1;
    }

    /*@Override
    public void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void onStop() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        super.onStop();
    }*/


    //绑定声明周期
    @Override
    public <T> LifecycleTransformer<T> bindToLife() {
        return bindToLifecycle();
    }

    protected abstract void setContentLayout();

    public void afterSetContent() {
    }

    //setContent之前做的一些操作
    public void beforeSetContent() {
    }


    public int getLayoutId() {
        return layoutId;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null)
            mPresenter.detachView();
    }


    protected abstract int setLayoutId();

    protected abstract T initPresenter();

    protected abstract void initView();

    protected abstract void initEvent();

}
